package com.ahp.config;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

public class ScoringHelper {

	public static final String CATEGORY = "Category Prioritization";

	public static void projection(Properties percentProps,
			float[][] preferenceMatrix, PrintWriter out,
			PriorityType priorityType) {

		out.println("<BR><H1>" + priorityType.getLabel()
				+ " PRIORITIZATION</H1><BR>");

		String[] criteria = priorityType.getCriteriaLabels();

		List<Float> totals = new ArrayList<Float>();

		out.println("<BR><H2>SIGNIFICANT CATEGORY</H2><BR>");
		int elements = preferenceMatrix.length;

		out.println("<table border='2' width='" + ((10 * elements) + 10)
				+ "%'><tr><td width='10%'>");
		for (int i = 0; i < elements; i++) {
			out.println(criteria[i] + "</td><td width='10%'>");
			int j;
			float sum = 0;
			for (j = 0; j < elements - 1; j++) {
				out.println(preferenceMatrix[i][j] + "</td><td width='10%'>");
				sum += preferenceMatrix[j][i];
			}
			sum += preferenceMatrix[j][i];
			totals.add(sum);
			out.println(preferenceMatrix[i][j]
					+ "</td></tr><tr><td width='10%'>");
		}
		out.println("Total:</td><td width='10%'>");
		int counter;
		for (counter = 0; counter < elements - 1; counter++) {
			out.println(totals.get(counter) + "</td><td width='10%'>");
		}
		out.println(totals.get(counter) + "</td></tr></table>");

		float weight[][] = preferenceMatrix;

		for (int i = 0; i < elements; i++) {
			for (int j = 0; j < elements; j++) {
				weight[i][j] = preferenceMatrix[i][j] / totals.get(j);
			}
		}
		float[] weightTotals = weight[0];
		float overallTotal = 0;

		out.println("<BR><H2>WEIGHTS ON OBJECTIVE CALCULATION</H2><BR>");
		out.println("<table border='2' width='" + ((10 * elements) + 20)
				+ "%'><tr><td width='10%'>");
		for (int i = 0; i < elements; i++) {
			float sum = 0;
			out.println(criteria[i] + "</td><td width='10%'>");
			for (int j = 0; j < elements; j++) {
				out.println(weight[i][j] + "</td><td width='10%'>");
				sum += weight[i][j];
			}
			weightTotals[i] = sum;
			overallTotal += sum;
			out.println(sum + "</td></tr><tr><td width='10%'>");
		}
		out.println("Total</td><td width='10%'>");
		for (int i = 0; i < elements; i++) {
			out.println("1.00000000</td><td width='10%'>");
		}
		out.println(overallTotal + "</td></tr></table>");

		out.println("<BR><H2>PERCENTAGE</H2><BR>");
		out.println("<table border='2' width='20%'><tr><td width='10%'>");
		String criteriaNames[] = priorityType.getCriteriaNames();
		for (int i = 0; i < elements; i++) {
			out.println(criteria[i] + "</td><td width='10%'>");
			out.println(weightTotals[i] + "</td></tr><tr><td width='10%'>");
			if (percentProps.get(criteriaNames[i]) != null) {
				percentProps
						.setProperty(criteriaNames[i], "" + weightTotals[i]);
			}
		}
		out.println("</td></tr></table>");

	}

	public static float[][] createPreference(HttpServletRequest req,
			PriorityType priorityType) {

		// Category
		float qualityByCost = Float.parseFloat(req
				.getParameter("qualityByCost"));
		float deliveryByCost = Float.parseFloat(req
				.getParameter("deliveryByCost"));
		float organizationByCost = Float.parseFloat(req
				.getParameter("organizationByCost"));
		float productDevelopmentByCost = Float.parseFloat(req
				.getParameter("productDevelopmentByCost"));
		float commercialTeamByCost = Float.parseFloat(req
				.getParameter("commercialTeamByCost"));
		float deliveryByQuality = Float.parseFloat(req
				.getParameter("deliveryByQuality"));
		float organizationByQuality = Float.parseFloat(req
				.getParameter("organizationByQuality"));
		float productDevelopmentByQuality = Float.parseFloat(req
				.getParameter("productDevelopmentByQuality"));
		float commercialTeamByQuality = Float.parseFloat(req
				.getParameter("commercialTeamByQuality"));
		float organizationByDelivery = Float.parseFloat(req
				.getParameter("organizationByDelivery"));
		float productDevelopmentByDelivery = Float.parseFloat(req
				.getParameter("productDevelopmentByDelivery"));
		float commercialTeamByDelivery = Float.parseFloat(req
				.getParameter("commercialTeamByDelivery"));
		float productDevelopmentByOrganization = Float.parseFloat(req
				.getParameter("productDevelopmentByOrganization"));
		float commercialTeamByOrganization = Float.parseFloat(req
				.getParameter("commercialTeamByOrganization"));
		float commercialTeamByProductDevelopment = Float.parseFloat(req
				.getParameter("commercialTeamByProductDevelopment"));
		float[][] categoryMatrix = {
				{ 1, 1 / qualityByCost, 1 / deliveryByCost,
						1 / organizationByCost, 1 / productDevelopmentByCost,
						1 / commercialTeamByCost },
				{ qualityByCost, 1, 1 / deliveryByQuality,
						1 / organizationByQuality,
						1 / productDevelopmentByQuality,
						1 / commercialTeamByQuality },
				{ deliveryByCost, deliveryByQuality, 1,
						1 / organizationByDelivery,
						1 / productDevelopmentByDelivery,
						1 / commercialTeamByDelivery },
				{ organizationByCost, organizationByQuality,
						organizationByDelivery, 1,
						1 / productDevelopmentByOrganization,
						1 / commercialTeamByOrganization },
				{ productDevelopmentByCost, productDevelopmentByQuality,
						productDevelopmentByDelivery,
						productDevelopmentByOrganization, 1,
						1 / commercialTeamByProductDevelopment },
				{ commercialTeamByCost, commercialTeamByQuality,
						commercialTeamByDelivery, commercialTeamByOrganization,
						commercialTeamByProductDevelopment, 1 } };

		// Cost
		float ipqByCrp = Float.parseFloat(req.getParameter("ipqByCrp"));
		float[][] costMatrix = { { 1, 1 / ipqByCrp }, { ipqByCrp, 1 } };

		// Organization
		float cpByFs = Float.parseFloat(req.getParameter("cpByFs"));
		float bvByFs = Float.parseFloat(req.getParameter("bvByFs"));
		float tiByFs = Float.parseFloat(req.getParameter("tiByFs"));
		float bvByCp = Float.parseFloat(req.getParameter("bvByCp"));
		float tiByCp = Float.parseFloat(req.getParameter("tiByCp"));
		float tiByBv = Float.parseFloat(req.getParameter("tiByBv"));
		float[][] organizationMatrix = {
				{ 1, 1 / cpByFs, 1 / bvByFs, 1 / tiByFs },
				{ cpByFs, 1, 1 / bvByCp, 1 / tiByCp },
				{ bvByFs, bvByCp, 1, 1 / tiByBv },
				{ tiByFs, tiByCp, tiByBv, 1 } };

		// Product Development
		float cqByDi = Float.parseFloat(req.getParameter("cqByDi"));
		float pltByDi = Float.parseFloat(req.getParameter("pltByDi"));
		float pltByCq = Float.parseFloat(req.getParameter("pltByCq"));
		float[][] productDevelopmentMatrix = { { 1, 1 / cqByDi, 1 / pltByDi },
				{ cqByDi, 1, 1/pltByCq }, { pltByDi, pltByCq, 1 } };

		// Delivery
		float vltByDlt = Float.parseFloat(req.getParameter("vltByDlt"));
		float flexibilityByDlt = Float.parseFloat(req
				.getParameter("flexibilityByDlt"));
		float flexibilityByVlt = Float.parseFloat(req
				.getParameter("flexibilityByVlt"));
		float[][] deliveryMatrix = { { 1, 1 / vltByDlt, 1 / flexibilityByDlt },
				{ vltByDlt, 1, 1/flexibilityByVlt },
				{ flexibilityByDlt, flexibilityByVlt, 1 } };

		// Commercial Team
		float pfuByCrt = Float.parseFloat(req.getParameter("pfuByCrt"));
		float[][] commercialTeamMatrix = { { 1, 1 / pfuByCrt }, { pfuByCrt, 1 } };
		
		// Flexibility
		float cfByDcf = Float.parseFloat(req.getParameter("cfByDcf"));
		float tpsByDcf = Float.parseFloat(req
				.getParameter("tpsByDcf"));
		float cfByTps = Float.parseFloat(req
				.getParameter("cfByTps"));
		float[][] flexibilityMatrix = { { 1, 1 / cfByDcf, 1 / tpsByDcf },
				{ cfByDcf, 1, 1/cfByTps },
				{ tpsByDcf, cfByTps, 1 } };

		// Technology & Innovation
		float iiByTi = Float.parseFloat(req.getParameter("iiByTi"));
		float[][] technologyAndInnovationMatrix = { { 1, 1 / iiByTi }, { iiByTi, 1 } };

		float[][] preferenceMatrix = null;
		switch (priorityType) {
		case CATEGORY:
			preferenceMatrix = categoryMatrix;
			break;
		case COST:
			preferenceMatrix = costMatrix;
			break;
		case ORGANIZATION:
			preferenceMatrix = organizationMatrix;
			break;
		case PRODUCT_DEVELOPMENT:
			preferenceMatrix = productDevelopmentMatrix;
			break;
		case DELIVERY:
			preferenceMatrix = deliveryMatrix;
			break;
		case COMMERCIAL_TEAM:
			preferenceMatrix = commercialTeamMatrix;
			break;
		case FLEXIBILITY:
			preferenceMatrix = flexibilityMatrix;
			break;			
		case TECHNOLOGY_AND_INNOVATION:
			preferenceMatrix = technologyAndInnovationMatrix;
			break;
		}
		return preferenceMatrix;
	}
}
